home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 14 / Hot Mix 14.iso / HTML / vendors / finesse / examples / sh / fkill < prev    next >
Text File  |  1996-06-27  |  3KB  |  151 lines

  1. #! /bin/sh
  2. # FINESSEAPPLICATIONKEY sSEpSIGAcBCQe
  3. #
  4. # Demo script for killing processes with sh script
  5.  
  6. . ${FINESSEPATH-/usr/local/finesse}/fsshinit
  7.  
  8. # First check for tty
  9.  
  10. tty -s || { echo "Need a tty. Exiting." ; exit 1 ; }
  11.  
  12. # get list of processes and PID column number
  13. #
  14.  
  15. list_proc() 
  16. {
  17. list=`( ps $opt | 
  18.         awk 'NR==1 { i=1; while ($i !~ /^PID$/) i++; 
  19.                      print "col="i;
  20.                      print " proclist='\''"; }
  21.           NR>1 && $0 !~ /awk/ && /'"$patt"'.*/ 
  22.           END   { print "'\''"; }'
  23.       ) 2>errfile`
  24. if [ -s errfile ] ; then
  25.   cat < errfile; /bin/rm -f errfile
  26.   exit 1 
  27. else
  28.   eval "$list"
  29.   /bin/rm -f errfile
  30. fi
  31. }
  32.  
  33. # update list of processes in kill window
  34. #
  35.  
  36. update_list()
  37. {
  38.   deletelist='*[delete]'
  39.   list_proc
  40.   addlist="`echo "$proclist" | 
  41.             awk 'NR>1{ print $0"[add]" }'`"
  42.   updatearg="-v"
  43.   updatelist="procsinout=$deletelist$cr$addlist"
  44.   windowarg=
  45.   windowlist=
  46. }
  47.  
  48. # check input
  49. #
  50.  
  51. case "$1" in
  52.   ?|help) 
  53.     echo "Usage: fkill \
  54. ['<psoptions>'] [<pattern>]" >&2
  55.     exit 2 ;;
  56.   -*) opt=$1; comm="ps $opt" ; shift ;;
  57.   *)  comm=ps;;
  58. esac
  59. patt="$1"
  60.  
  61. # get initial list of processes
  62. # and declare window 
  63. #
  64.  
  65. # Start two demo processes to kill
  66.  
  67. sleep 60 &
  68. sleep 60 &
  69.  
  70. list_proc
  71. cr="
  72. "
  73. windef="
  74.   FsWindow   -name killwin
  75.              -title `basename $0`;
  76.   FsSeparator -line no;
  77.   FsForm     -name form1
  78.              -orientation horizontal
  79.              -packing tight
  80.              -spacing 30;
  81.   FsText     -label Options: -parent form1 
  82.              -var opt='$opt' -spacing 10
  83.              -bdefault update;
  84.   FsText     -label Pattern: -name patt
  85.              -parent form1 -var patt='$patt'
  86.              -spacing 10 -bdefault update;
  87.   FsRadio    -label Signals:  
  88.              -items 'INT KILL TERM' 
  89.              -var signal==TERM;
  90.   FsSeparator;
  91.   FsSeparator;
  92.   FsSeparator -line no;
  93.   FsList     -label 'Select processes to kill:' 
  94.          -items '$proclist'
  95.              -nvisible 10
  96.              -mode multiple
  97.          -inputsep '$cr'
  98.          -outputsep '$cr'
  99.          -var procsinout=''
  100.          -expert k;
  101.   FsSeparator;
  102.   FsPushButton -label Kill -name kill
  103.                -fsbutton k -winstat touch;
  104.   FsPushButton -label Update -name update
  105.                -fsbutton u -winstat touch;
  106.   FsPushButton -label Exit 
  107.                -fsbutton e -nrows 1;"
  108.  
  109. # Open server and display window
  110. #
  111.  
  112. Fsopen -o 3 "$@"                                
  113.  
  114. # Kill selected processes and/or update list
  115. #
  116.  
  117. updatearg=
  118. updatelist=
  119. windowarg="-w"
  120. windowlist="$windef"
  121.  
  122. while :
  123. do
  124.  Fsdisplay "$windowarg" "$windowlist"\
  125.            "$updatearg" "$updatelist"\
  126.            -m "List and kill processes"\
  127.            -n killwin\
  128.            -v opt=$opt\
  129.            -v patt=$patt
  130.  case $fsbutton in
  131.   k) case $signal in
  132.        INT) SIG=2 ;; 
  133.        KILL) SIG=9 ;; 
  134.        TERM) SIG=15 
  135.      esac
  136.      if [ "$procsinout" != "" ] ; then
  137.        kill -$SIG `echo "$procsinout" | 
  138.        awk '{ print $'$col'}'` 
  139.        update_list
  140.      else
  141.        updatelist=
  142.        updatearg=
  143.        Fsecho "No processes selected..."
  144.      fi ;;
  145.   u) update_list ;;
  146.   e|a) Fsclose; exit ;;
  147.  esac
  148.  windowarg=
  149.  windowlist=
  150. done
  151.